package com.version1_3.controller;

import com.util.format.MapToJson;
import com.version1_2.controller.PageController1_2;
import com.version1_3.service.CoinService;
import com.version1_3.service.ShareService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by kuibu on 16/1/26.
 */

@Controller
public class ShareController {
    private static final Logger LOGGER = Logger.getLogger(ShareController.class);

    @Autowired
    private PageController1_2 pageController12;

    @Autowired
    private ShareService shareService;

    @Autowired
    private CoinService coinService;

    /**
     * 在APP外记录文章被用户阅读
     * <p>
     * 调用接口: http://www.kuibutech.com/kuibuversion1/version1_3/share/getPageInfo/{pageId}
     */
    @RequestMapping(value = "/version1_3/share/getPageInfo/{pageId}", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getPageInfo(@PathVariable("pageId") int pageId) {

        shareService.doRecordPageReaded(pageId);
        return pageController12.getPageInfoShare(pageId);
    }

    /**
     * 分享App - jsp视图
     *
     * @return
     */
    @RequestMapping(value = "/share/shareApp", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView shareApp() {
        return new ModelAndView("/kuibuintroduce");
    }

    /**
     * 记录用户分享App,增加用户积分
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/share/shareApp/{userId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String recordShareApp(@PathVariable("userId") int userId) {
        boolean recordUserShareAppResult = coinService.recordUserShareApp(userId);
        return new MapToJson("recordShareResult", recordUserShareAppResult).toJsonString();
    }

    /**
     * 公司手机官网注册版 - jsp视图
     * <p>
     * 地址: http://www.kuibutech.com/kuibuversion1/kuibu/introduce
     *
     * @return
     */
    @RequestMapping(value = "/kuibu/introduce", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView introduceKuibu() {
        return new ModelAndView("/guanwang/kuibuGW");
    }


    /**
     * 第三方平台下载跬步 - jsp视图
     * <p>
     * 地址: http://www.kuibutech.com/kuibuversion1/kuibu/third/introduce/{platform}
     *
     * @return
     */
    @RequestMapping(value = "/kuibu/third/introduce/{platform}", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView introduceKuibuThird(@PathVariable("platform") String platform) {
        ModelAndView modelAndView = new ModelAndView("/cooperation/JianZhiCat/kuibuintroduce");
        modelAndView.addObject("platform", platform);
        return modelAndView;
    }

    /**
     * 网页版登陆 - jsp视图
     * <p>
     * 地址: http://www.kuibutech.com/kuibuversion1/kuibu/login
     *
     * @return
     */
    @RequestMapping(value = "/kuibu/login", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView htmlLogin() {
        return new ModelAndView("/guanwang/login");
    }

    /**
     * 网页版注册 - jsp视图
     * <p>
     * 地址: http://www.kuibutech.com/kuibuversion1/kuibu/register
     *
     * @return
     */
    @RequestMapping(value = "/kuibu/register", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView htmlRegister() {
        return new ModelAndView("/guanwang/register");
    }


    /*app内点击邀请码功能,转到页面,需要向页面发送一些信息*/

    /**
     * 邀请码填写界面 - jsp视图
     *
     * @return
     */
    @RequestMapping(value = "/share/invitePage/{userId}", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView invitePage(@PathVariable("userId") int userId) {

        Map userInviteMap = shareService.getUserInviteInfo(userId);
        ModelAndView invitePage = new ModelAndView("/invitePage");
        boolean hasInputInviteCode = shareService.getUserHasInputInviteCode(userId);
        if (hasInputInviteCode) {
            invitePage.addObject("hasInputInviteCode", true);
            invitePage.addObject("inviteCode", userInviteMap.get("inviteCode"));
            invitePage.addObject("invitedFriends", userInviteMap.get("invitedFriends"));
        } else {
            invitePage.addObject("hasInputInviteCode", false);
            invitePage.addObject("inviteCode", userInviteMap.get("inviteCode"));
            invitePage.addObject("invitedFriends", userInviteMap.get("invitedFriends"));
            invitePage.addObject("userId", userId);
        }

        return invitePage;
    }

    /**
     * 提交邀请码
     *
     * @return
     */
    @RequestMapping(value = "/share/submitInvite", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String submitInvite(HttpServletRequest request) {
        String inviteCode = request.getParameter("inviteCode");
        int userId = Integer.parseInt(request.getParameter("userId"));

        return shareService.submitInvite(userId, inviteCode);
    }
}
